package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper,Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Transactional
    @Override
    public void saveWithFlavor(DishDto dishDto) {

        this.save(dishDto);
        Long id = dishDto.getId();

        //获取到菜品的口味集合
        List<DishFlavor> dishFlavors =dishDto.getFlavors();

        for (DishFlavor dishFlavor : dishFlavors) {
            dishFlavor.setDishId(id);
        }

        dishFlavorService.saveBatch(dishFlavors);

    }


    //根据Id查询菜品的信息和对应的口味信息
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品的信息，从dish中查询你
        Dish dish  = this.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);

        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);

        return dishDto;

    }

    @Override
    public void updateWithFlavor(DishDto dishDto) {
        this.updateById(dishDto);

        Long dishId = dishDto.getId();

        //清理当前菜品对应的口味数据
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);


        //添加当前提交过来的口味数据
        List<DishFlavor> flavors = dishDto.getFlavors();


        //将dishId存入对应的口味字段中
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishId);
        }

        dishFlavorService.saveBatch(flavors);
    }

    public void deleteWithFlavor(String ids){

        String[] splitedIds = ids.split(",");
        //清理当前菜品
        this.removeByIds(Arrays.asList(splitedIds));

        //清理当前菜品对应的口味数据
        dishFlavorService.removeByIds(Arrays.asList(splitedIds));
    }


    //批量启售
    @Override
    public void batchStartSeal(String ids) {

        String[] splitedIds = ids.split(",");
        UpdateWrapper<Dish> wrapper = new UpdateWrapper<>();
        wrapper.in("id",splitedIds).set("status",1);
        this.update(null,wrapper);
    }


    //批量停售
    @Override
    public void batchStopSeal(String ids) {
        String[] splitedIds = ids.split(",");
        UpdateWrapper<Dish> wrapper = new UpdateWrapper<>();
        wrapper.in("id",splitedIds).set("status",0);
        this.update(null,wrapper);
    }
}
